import 'package:flutter/material.dart';
import 'package:flutter_again/parsers/implementations/javmenu_parser.dart';
import 'package:flutter_again/services/video_platform_manager.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
// import 'package:flutter_again/utils/log.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({ super.key });

  @override
  State<SettingPage> createState() => _SettingPage();
}

class _SettingPage extends State<SettingPage> {
  late VideoPlatformManager platformManager;
  var hasJavmenu = false;
  var javmenuParser = JavmenuParser();
  final asyncPrefs = SharedPreferencesAsync();
  final token = 'hasJavmenu';

  @override
  void initState() {
    super.initState();
    platformManager = context.read<VideoPlatformManager>();
    initConfig();
  }

  void initConfig() async {
    var val = await asyncPrefs.getBool('hasJavmenu') ?? false;
    if (val) {
      platformManager.registerParser(javmenuParser);
      setState(() {
        hasJavmenu = val;
      });
    }
  }

  void changeJavmenuParser(bool value) {
    if (value) {
      platformManager.registerParser(javmenuParser);
    } else {
      platformManager.removeParser(javmenuParser.platformId);
    }
    asyncPrefs.setBool(token, value);
  }

  @override
  Widget build(BuildContext context) {
    
    return  SafeArea(
      child: Column(
        children: [
          ListTile(
            title: const Text('javmenu parser'),
            trailing: Switch(
              value: hasJavmenu,
              onChanged: (bool value) {
                setState(() {
                  hasJavmenu = value;
                });
                changeJavmenuParser(value);
              }
            ),
          )
        ],
      ),
    );
  }
}
